<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="exit">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          router
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#00ffff"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="v.id + ''" v-for="v of menuList" :key="v.id">
            <!-- 一级菜单的模板 -->
            <template slot="title">
              <i :class="iconsObj[v.id]"></i>
              <span>{{ v.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + v.path"
              v-for="v of v.children"
              :key="v.id"
              @click="saveNavState('/' + v.path)"
            >
              <!-- 二级菜单的模板 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ v.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    // 发送数据请求获取导航栏数据
    async getMenuList() {
      let res = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        this.$message({
          type: 'error',
          message: res.meta.message,
          duration: 1500
        })
        return false
      } else {
        this.menuList = res.data
      }
    },
    //点击按钮退出登录
    exit() {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    //点击按钮切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的状态
    saveNavState(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  mounted() {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  }
}
</script>

<style scoped lang="less">
.el-container.home-container {
  height: 100%;
  .el-header {
    padding-left: 0px;
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-container {
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
    .el-aside {
      background-color: #333744;
      .el-menu {
        border-right: none;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
}
.iconfont {
  margin-right: 5px;
}
</style>
